﻿namespace VfAnswersMy
{
    using System;
    using System.Reactive.Disposables;
    using System.Windows.Forms;
    using JanusLibAing;
    using Rsdn.Janus;
    using Rsdn.Janus.CoreServices.Extensibility;
    using Rsdn.Janus.CoreServices.NavigationTree;
    using Rsdn.Janus.CoreServices.NavigationTree.Extensibility;
    using Rsdn.SmartApp;

    [VirtualForumProvider]
    public class AnswersMyProvider : IVirtualForumProvider
    {
        private readonly IServiceProvider provider;
        private ISharedInfoService sharedInfoService;

        private AnswersMyItemInfo forumInfo = new AnswersMyItemInfo();

        public AnswersMyProvider(IServiceProvider provider)
        {
            this.provider = provider;
            if (provider == null)
            {
                throw new ArgumentNullException("provider");          //MLHIDE
            }

            sharedInfoService = provider.GetRequiredService<ISharedInfoService>();

        }

        #region Implementation of INavigationTreeProvider

        public Func<object, bool> IsUpdateNeedFunc
        {
            get { return IsUpdateNeed; }
        }

        public Action<int, MessageCounters> LoadCountersAction
        {
            get { return LoadCounters; }
        }

        public Func<bool, MessageCounters, IMsg> LoadTopicsAsChildrenFunc
        {
            get { return LoadTopicsAsChildren; }
        }

        public IServiceProvider ServiceProvider
        {
            get { return provider; }
        }

        public INavigationTreeItemInfo GetItemInfo(string itemId)
        {
            return forumInfo;
        }

        public Control CreateGuiControl()
        {
           return null;
        }

        #endregion

        public bool IsUpdateNeed(object msgIn)
        {
            bool ret = false;

            if (msgIn is Msg)
            {
                // don't move to class member we need actual value
                int selfId = sharedInfoService.GetIntValue(SharedInfoKeys.SelfId) ?? 0;

                Msg msg = msgIn as Msg;
                ret = msg.UserID == selfId;
            }

            return ret;
        }

        /// <summary>
        /// Loads the counters only.
        /// </summary>
        /// <param name="id"> The forum id. </param>
        /// <param name="messageCounters"> The message Counters. </param>
        internal void LoadCounters(int id, MessageCounters messageCounters)
        {
            AnswersMyMessage root = new AnswersMyMessage(ServiceProvider);
            using (new WaitCursor())
            {
                root.FillCounters();
            }
#if FRM_UPDATE_TRACE
            Trace.WriteLine("FRU:MyAnswers.LoadCounters");            //MLHIDE
#endif
            messageCounters.MessagesCount = root.RepliesCount;
            messageCounters.Unread = root.UnreadCount;
            messageCounters.RepliesToMeUnread = root.UnreadCount;
        }

        /// <summary>
        /// Loads all topics as children in message.
        /// </summary>
        /// <param name="isLoadAll">if set to <c>true</c> [is load all].</param>
        /// <returns>root message</returns>
        protected IMsg LoadTopicsAsChildren(bool isLoadAll, MessageCounters messageCounters)
        {
            AnswersMyMessage root = new AnswersMyMessage(ServiceProvider);
            using (new WaitCursor())
            {
                root.FillChildrenWithMessages();
#if FRM_UPDATE_TRACE
                Trace.WriteLine(
                    string.Format(
                        "FRM:My answers:RepliesToMe Unread {0}, Unread {1}, summary {2}", //MLHIDE
                        root.RepliesToMeUnread,
                        root.Unread,
                        root.Count));
#endif
                messageCounters.MessagesCount = root.Count;
                messageCounters.Unread = root.Unread;
                messageCounters.RepliesToMeUnread = root.RepliesToMeUnread;
            }
            return root;
        }

    }
}

